#!/bin/bash

#
# Installation of Sogou Pinyin inputting method.
# 
# Target platform: Based on Ubuntu(Ubuntu Kylin/Ubuntu Desktop/Linux Mint/ZorinOS/DeepinOS), include graphical interface, and not applicable to any server version.
# Create date: 2019/07/07
# Create user: SteveJrong
# File version: 1.0.0

# Import some shells.
. common/common.sh

# ######################### start of variables #########################
APP_PACKAGE_KEYWORD_1="sogoupinyin"
APP_PACKAGE_SUFFIX="deb"
APP_PACKAGE_DOWNLOAD_LINK="http://cdn2.ime.sogou.com/dl/index/1524572264/sogoupinyin_2.2.0.0108_amd64.deb?st=cAk0effmV87SZHNm5Tv95w&e=1562514121&fn=sogoupinyin_2.2.0.0108_amd64.deb"
# ######################### end of variables #########################

# ######################### start of functions #########################
# ######################### end of functions #########################

# ######################### start of install Sogou Pinyin inputting method #########################
echo '------------------------- Starting install Sogou Pinyin inputting method.'

echo '------------------------- Installation status checked.'
installationStatus=`sudo dpkg -l | grep $APP_PACKAGE_KEYWORD_1`
if [ -n "$installationStatus" ]; then
	# App package already installed, need to uninstall it.
	echo -e "------------------------- App package already installed, uninstall it first."

	fullName=
	eval findAppPackageFullNameByKeyword $APP_PACKAGE_KEYWORD_1 $APP_PACKAGE_INSTALLATION_MODE_DPKG_APT fullName

	if [[ -n $fullName ]]; then
		echo -e "------------------------- Starting uninstall old app package:" $fullName
		sudo apt-get remove --purge $fullName

        echo -e "------------------------- Starting uninstall old dependencies of this app package."
        sudo apt-get purge ibus
        sudo apt-get autoremove
	else
		outputWarnMsg "App package not installation or query failure!"
	fi
	
fi
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Checking temporary file path.'
if [ ! -d "$TEMP_FILE_PATH" ]; then
	# Temporary directory does not exist, need to create it.
	echo '------------------------- Temporary directory does not exist, create it first.'

	sudo mkdir -p $TEMP_FILE_PATH
	sudo chmod -R 777 $TEMP_FILE_PATH
else
	# Temporary directory already exists, need to check of old temporary files.
	checkOldTemporaryFiles=`find $TEMP_FILE_PATH -iname "*$APP_PACKAGE_KEYWORD_1*.$APP_PACKAGE_SUFFIX"`
	if [ -f "$checkOldTemporaryFiles" ]; then
		echo '------------------------- Application package file already exists, delete it first.'

		# Application package file already exists, need delete first.
		sudo rm -f $checkOldTemporaryFiles
	fi
fi
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Downloading application package.'
wget -P $TEMP_FILE_PATH $APP_PACKAGE_DOWNLOAD_LINK
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Installing dependencies for application package.'
# Because Sogou Pinyin inputting method needs dependencies, install dependencies first.
sudo apt-get -f install
sudo apt-get install fcitx-libs fcitx-libs-qt libqtwebkit4
echo -e "------------------------- Done.\n"

echo '------------------------- Installing application package.'
newTemporaryFiles=`find $TEMP_FILE_PATH -iname "*$APP_PACKAGE_KEYWORD_1*.$APP_PACKAGE_SUFFIX"`
sudo dpkg -i $newTemporaryFiles
echo -e "------------------------- Done.\n"

outputSuccessMsg "Install Sogou Pinyin inputting method is complete!"

read -p 'Now you need to restart your computer. Do you want to continue?[Y/n]' rebootComputerOfcomplete
case $rebootComputerOfcomplete in
    [yY]*)
        echo '------------------------- Ready to restart your computer.'
        sudo reboot
        ;;
    [nN]*)
        outputErrorMsg "The operation was aborted, exit!"
        exit
        ;;
    *)
        outputErrorMsg "Illegal parameters, exit!"
esac
# ######################### end of install Sogou Pinyin inputting method #########################